function createBrowserHistory(props) {
  let globalHistory = window.history;
  let listeners = [];
  let currentMessage;
  let userConfirm = props.getUserConfirmation
    ? props.getUserConfirmation
    : window.confirm;
  function go(n) {
    globalHistory.go(n);
  }
  function goForward(n) {
    globalHistory.goForward(n);
  }
  function goBack(n) {
    globalHistory.goBack();
  }
  function listen(listener) {
    listeners.push(listener);
    return function () {
      // 取消监听函数，如果调它的话，会把此监听函数从数组中删除
      listeners = listeners.filter((l) => l !== listener);
    };
  }
  window.addEventListener("popstate", (event) => {
    setState({
      action: "POP",
      location: {
        state: event.state,
        pathname: window.location.pathname,
      },
    });
  });
  function setState(newState) {
    Object.assign(history, newState);
    history.length = globalHistory.length;
    listeners.forEach((listener) => listener(history.location));
  }
  function push(to, nextState) {
    const action = "PUSH";
    let pathname;
    let state;
    if (typeof to === "object") {
      state = to.state;
      pathname = to.pathname;
    } else {
      pathname = to;
      state = nextState;
    }
    if (currentMessage) {
      let message = currentMessage({ pathname });
      let allow = userConfirm(message);
      if (!allow) return;
    }
    // 对标history pushState
    globalHistory.pushState(state, null, pathname);
    let location = { state, pathname: pathname };
    setState({ action, location });
  }
  function block(newMessage) {
    currentMessage = newMessage;
    return () => (currentMessage = null);
  }
  const history = {
    action: "POP", // 对history执行的动作
    push,
    go,
    goBack,
    goForward,
    listen,
    location: {
      pathname: window.location.pathname,
      state: globalHistory.state,
    },
    block,
  };
  return history;
}
export default createBrowserHistory;
